package cn.zjh.system.service.impl;

import cn.zjh.system.domain.Menu;
import cn.zjh.system.mapper.MenuMapper;
import cn.zjh.system.service.IMenuService;
import cn.zjh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> menuTree() {

            //将遍历出的部门都装到map集合当中
            List<Menu> mapperAll = menuMapper.findAll();
            HashMap<Long, Menu> map = new HashMap<>();
            for (Menu menu:mapperAll){
                map.put(menu.getId(),menu);
            }

            List<Menu> peatTree = new ArrayList<>();

            for(Menu menu : mapperAll){
                if (menu.getParentId() == null){//获取所有的顶级部门
                    peatTree.add(menu);
                }else {
                    Long parentId = menu.getParentId();

                    Menu parentDepartment = map.get(parentId);

                    parentDepartment.getChildren().add(menu);
                }
            }

            return peatTree;
        }

    }
